package com.oa.controller;

import java.util.Arrays;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.oa.bean.WorkPlan;
import com.oa.service.WorkPlanService;

@Controller
public class WorkPlanController {
	
	@Resource
	WorkPlanService workPlanService;
	
	@RequestMapping("addWorkPlan")
	public String addWorkPlan(WorkPlan plan,HttpSession session){
		plan.setUpost_id((int)session.getAttribute("uid"));
		System.out.println(plan);
		Integer result = workPlanService.addWorkPlan(plan);
		if(result!=0){
			return "redirect:Template/workplan.jsp";
		}
		return "redirect:Template/workplan.jsp";
	}
	
	@RequestMapping("getWorkPlan")
	@ResponseBody
	public Map<String,Object> getWorkPlan(String title,String endDate,String startDate,Integer currentPage,HttpSession session){
		System.out.println("title+++"+title+"endDate="+endDate+"startDate="+startDate+"currentPage="+currentPage);
		Integer uid = (Integer) session.getAttribute("uid");
		System.out.println(uid);
		Map<String,Object> map = workPlanService.getWorkPlanList(title, endDate, startDate, currentPage,uid);
		return map;
	}
	
	@RequestMapping("changePlanStart")
	@ResponseBody
	public String changePlanStart(Integer pid){
		String result = workPlanService.updateWorkPlan(pid);
		return result;
	}
	
	@RequestMapping("getPlanByid")
	@ResponseBody
	public WorkPlan getPlanByid(Integer pid){
		WorkPlan plan = workPlanService.getPlanOne(pid);
		return plan;
	}
	
	@RequestMapping("changePlanWork")
	public String changePlanWork(WorkPlan plan){
		System.out.println(plan);
		String result = workPlanService.changeWorkPlan(plan);
		System.out.println(result);
		if(result.equals("true")){
			return "redirect:Template/workplan.jsp";
		}
		return null;
	}

	@RequestMapping("deletePlan")
	@ResponseBody
	public String deletePlan(String str){
		System.out.println(str);
		String[]arr=str.split(",");
		int[]array = new int[2];
		for(int i=0;i<arr.length;i++){
			array[i]=Integer.parseInt(arr[i]);
		}
		System.out.println(Arrays.toString(array));
		String result = workPlanService.deleteWorkPlanArray(array);
		return result;
	}
	
	@RequestMapping("getMyPlan")
	@ResponseBody
	public Map<String,Object> getMyPlan(Integer currentPage,HttpSession session){
		System.out.println(currentPage);
		String name = (String) session.getAttribute("uname");
		Map<String,Object> map = workPlanService.getMyJoinPlan(name, currentPage);
		return map;
	}
	
}
